<template>
  <div>
    <el-upload
      action="/rel/webapi/user/upload"
      list-type="picture-card"
      :limit="9"
      :headers="headers"
      :disabled="disabled"
      :file-list="fileList"
      :before-upload="uploadBeforeAction"
      :on-success="uploadSuccessAction"
      :on-progress="uploadProcessAction"
      :on-error="uploadErrorAction"
      :class="disabled ? 'upload-add-class' : ''"
      ref="uploadRef"
    >
      <i slot="default" class="el-icon-plus"></i>
      <div slot="file" slot-scope="{ file }">
        <img class="el-upload-list__item-thumbnail" :src="file[urlKey]" />
        <span class="el-upload-list__item-actions">
          <span class="el-upload-list__item-preview" @click="handlePreviewAction(file)">
            <i class="el-icon-zoom-in"></i>
          </span>
          <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemoveAction(file)">
            <i class="el-icon-delete"></i>
          </span>
        </span>
      </div>
    </el-upload>

    <Dialog
      v-if="dialogVisible"
      :show.sync="dialogVisible"
      title="图片预览"
      :escClose="true"
      width="800px"
      @close="close()"
    >
      <div style="height: 20px"></div>
      <img width="100%" :src="dialogImageUrl" />
      <div style="height: 20px"></div>
    </Dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import Dialog from '@/components/dialog/index.vue'

export default {
  components: { Dialog },
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    fileList: {
      type: Array,
      default: () => []
    },
    urlKey: {
      type: String,
      default: 'url'
    }
  },
  computed: {
    ...mapState({
      token: (state) => state.common.token
    }),
    headers() {
      return {
        'X-Access-Token': this.token
      }
    }
  },
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false
    }
  },
  methods: {
    uploadSuccessAction(res, file) {
      const { code, result } = res
      if (code === 200) {
        this.$emit('uploaded', { uid: file.uid, url: result })
      } else {
        this.$message({ type: 'error', message: '上传失败' })
        this.handleRemoveAction(file)
      }
    },

    uploadBeforeAction(file, fileList) {},

    uploadErrorAction(errMsg, file, fileList) {
      this.handleRemoveAction(file)
    },

    uploadProcessAction(event, file, fileList) {},

    handleRemoveAction(file) {
      const fileList = this.$refs.uploadRef.uploadFiles
      for (let index = 0; index < fileList.length; index++) {
        if (fileList[index] === file) {
          fileList.splice(index, 1)
          this.$emit('removed', file.uid)
          break
        }
      }
    },

    handlePreviewAction(file) {
      this.dialogImageUrl = file[this.urlKey]
      this.dialogVisible = true
    },

    close() {
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped lang="scss">
.upload-add-class {
  /deep/ .el-upload--picture-card {
    display: none;
  }
}

/deep/ .el-upload-list__item {
  div {
    width: 100% !important;
    height: 100% !important;

    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
  }
}
</style>
